<?php

namespace App\Admin\Actions\Form;

use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;
use Dcat\Admin\Admin;
use Maatwebsite\Excel\Facades\Excel;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Contracts\LazyRenderable;
use App\Imports\LiveTextImport;


class LiveTextUploadForm extends Form implements LazyRenderable
{

    use LazyWidget;

    // 处理表单提交请求
    public function handle(array $input)
    {
        $boxId = $this->payload['boxId'];

        $path = public_path('uploads/');
        $file = $path.$input['csv'];

        Excel::import(new LiveTextImport($boxId), $file);
        unlink($file);

        return $this->response()->success('导入成功.')->refresh();
    }

    // 构建表单
    public function form()
    {
        $this->confirm('您确定要上传该文件吗', '');
        $this->file('csv', '文件')->uniqueName()->autoUpload()->accept('csv,xls,xlsx')->required();
    }
}